package com.shuffling;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * 轮播适配器
 * Created by ${oushuhua} on 2017/7/18.
 */

public class MyViewPagerAdapter extends PagerAdapter {

    private Context mContext;
    private List<Bitmap> mBitmap = new ArrayList<Bitmap>();

    public MyViewPagerAdapter(Context ctx, List<Bitmap> mBitmap) {
        mContext = ctx;
        this.mBitmap= mBitmap;
    }

    public int getCount(){
        /**
         * 返回图片的数量
         * 如果要使viewpager循环，直接返回比mImageIds.length大的值就可以了
         */
        return mBitmap.size();
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        if (position>=mBitmap.size()){
            return null;
        }
        Bitmap bitmap =mBitmap.get(position);
        imageView.setImageBitmap(bitmap);
        container.addView(imageView);
        return imageView;
    }
    /**
     * 移除ViewPager中的View
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}
